import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/address_api.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:flutter/material.dart';

class AddressEditAndNew extends StatefulWidget {
  final int id; //标记，1为新建地址，2为编辑地址
  final TextEditingController addressController;
  final TextEditingController nameController;
  final TextEditingController phoneController;
  final Function saveInfoAndCommit;
  final String oldaddress;
  final String oldname;
  final String oldphone;
  final num addressid;
  AddressEditAndNew(
      {Key key,
      @required this.addressController,
      @required this.nameController,
      @required this.phoneController,
      @required this.oldaddress,
      @required this.oldname,
      this.oldphone,
      @required this.saveInfoAndCommit,
      @required this.id,
      this.addressid})
      : super(key: key);

  @override
  _AddressEditAndNewState createState() => _AddressEditAndNewState();
}

class _AddressEditAndNewState extends State<AddressEditAndNew> {
  @override
  void initState() {
    super.initState();
    widget.addressController.text = widget.oldaddress;
    widget.nameController.text = widget.oldname;
    widget.phoneController.text = widget.oldphone;
  }

  Future _newAddress() async {
    await newAddress_api('$backIP/address', {
      "value": widget.addressController.text,
      "number": widget.phoneController.text,
      "name": widget.nameController.text
    });
    print(widget.addressController.text);
  }

  Future _editAddress() async {
    await editAddress_api('$backIP/address', {
      "id": widget.addressid,
      "value": widget.addressController.text,
      "number": widget.phoneController.text,
      "name": widget.nameController.text
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Container(
          margin: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextField(
                controller: widget.addressController,
                decoration: InputDecoration(labelText: '收货地址'),
              ),
              TextField(
                controller: widget.nameController,
                decoration: InputDecoration(labelText: '姓名'),
              ),
              TextField(
                controller: widget.phoneController,
                decoration: InputDecoration(labelText: '电话'),
              ),
              Container(
                margin: EdgeInsets.only(left: 20, right: 20, top: 30),
                width: double.infinity,
                height: 40,
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        colors: [Colors.lightBlueAccent, Colors.blueAccent]),
                    borderRadius: BorderRadius.circular(20)),
                child: RaisedButton(
                  child: Text(
                    '保存',
                    style: TextStyle(
                        color: Colors.white, fontWeight: FontWeight.w100),
                  ),
                  color: Colors.transparent,
                  elevation: 2,
                  highlightElevation: 0,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20)),
                  onPressed: () {
                    if (widget.addressController.text == '' ||
                        widget.nameController.text == '' ||
                        widget.phoneController.text == '') {
                      showDialog(
                        context: context,
                        builder: (context) => SimpleDialog(
                          title: Text('提示'),
                          children: [
                            Container(
                              margin: EdgeInsets.only(left: 10),
                              child: Text('请将内容填写完整!'),
                            ),
                            Align(
                              alignment: Alignment.centerRight,
                              child: FlatButton(
                                onPressed: () {
                                  Navigator.pop(context);
                                },
                                child: Text(
                                  '取消',
                                  style: TextStyle(color: Colors.blue),
                                ),
                              ),
                            )
                          ],
                        ),
                      );
                    } else {
                      if (widget.id == 1) {
                        _newAddress();
                      } else {
                        _editAddress();
                      }
                      openAlertDialog(
                          context, widget.saveInfoAndCommit, '保存成功');
                    }
                  },
                ),
              )
            ],
          ),
        )
      ],
    );
  }
}
